#include<iostream>
#include<unistd.h>
#include<fcntl.h>
#include<cerrno>
#include<cstring>
using namespace std;
bool SetNonBlock(int fd)
{
    int fl = fcntl(fd,F_GETFL);
    if(fl < 0)
    {
        cerr<<"fcntl error" << endl;
        return false;
    }
    fcntl(fd,F_SETFL,fl | O_NONBLOCK);
    return true;
}
int main()
{
    if(SetNonBlock(0) == false)
        return 1;
    char buffer[1024];
    while(true)
    {
        int size = read(0,buffer,sizeof(buffer)-1);
        if(size < 0)
        {
            if(errno == EAGAIN || errno == EWOULDBLOCK)//底层数据没有准备好
            {
                cout << strerror(errno) << endl;
                sleep(1);
                continue;
            }
            else if(errno == EINTR)//在读取数据前被信号打断
            {
                cout << strerror(errno) << endl;
                sleep(1);
            }
            else
            {
                cerr << "read error" << endl;
                break;
            }
        }
        else
        {
            buffer[size] = '\0';
            cout << "echo: " << buffer;
        }
    }
    return 0;
}